package org.management.system.controller;


import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.management.base.BaseController;
import org.management.base.data.PermissionCode;
import org.management.base.data.Result;
import org.management.page.Pagination;
import org.management.system.entity.RoleEntity;
import org.management.system.service.ResourceService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * 角色控制层
 * @author jiangyan
 *
 */
@RestController
@Api(tags="角色模块")
@RequestMapping(value = "/roles")
public class RoleController extends BaseController<RoleEntity> {

    @Resource
    private ResourceService resourceService;

    @GetMapping
    @ApiOperation("获取角色集合")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page",value = "分页信息",dataType = "Pagination"),
        @ApiImplicitParam(name = "entity",value = "角色实体信息",dataType = "RoleEntity")
    })
    @RequiresPermissions(value = PermissionCode.USER_READ_LIST)
    @Override
    public Result getEntities(Pagination<RoleEntity> page, RoleEntity entity) {
        entity.setIsOpen(true);
        return super.getEntities(page, entity);
    }

    /**
     * 保存角色及关联资源
     * @param id 角色id
     * @param resourceIds 资源集合
     * @return ResultData
     */
    @PostMapping(value="/{id}/resources")
    @ApiOperation("保存角色及关联资源")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",value = "角色ID",dataType = "String",required = true),
        @ApiImplicitParam(name = "resourceIds",value = "资源组",dataType = "String",required = true)
    })
    @RequiresPermissions(value = PermissionCode.USER_WRITE_EDIT)
    public Result saveRoleResource(@PathVariable  String id, String resourceIds) {
        String [] resourceArray = resourceIds.split(",");
        this.resourceService.saveRoleResource(id,resourceArray);
        return Result.success("保存信息成功");
    }

    /**
     * 根据角色获取资源
     * @param id 角色ID
     * @return  List<ResourceEntity>
     */
    @GetMapping(value="/{id}/resources")
    @ApiOperation("根据角色ID获取资源信息")
    @ApiImplicitParam(name = "id",value = "角色ID",dataType = "String",required = true)
    @RequiresPermissions(value = PermissionCode.USER_READ_LIST)
    public Result getListByRoleId(@PathVariable String id){
        return Result.success("获取信息成功", this.resourceService.getListByRoleId(id));
    }
}